import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class WeappConfigService extends BaseService<any> {
  private readonly logger = new Logger(WeappConfigService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getWeappConfig
   * 对应 PHP: WeappConfigService_admin::getWeappConfig()
   * 逻辑类型: undefined - undefined
   */
  async getWeappConfig() {
    // 基于PHP真实逻辑: getWeappConfig
    // PHP原文: $config_info = (new CoreWeappConfigService())->getWeappConfig($this->site_id);         foreach ($config_info as $k => $v) {             if ($v !== '' ...
const config_info = this.coreWeappConfigService.getWeappConfig(this.site_id);
        foreach (config_info as k => v) {
            if (v !== '' && in_[k, ['app_secret', 'encoding_aes_key'))) {
                config_info[k] = CommonDict.ENCRYPT_STR;
            }
  }

  /**
   * setWeappConfig
   * 对应 PHP: WeappConfigService_admin::setWeappConfig()
   * 逻辑类型: undefined - undefined
   */
  async setWeappConfig(data: any[]) {
    // 基于PHP真实逻辑: setWeappConfig
    // PHP原文: $config = (new CoreWeappConfigService())->getWeappConfig($this->site_id);         foreach ($data as $k => $v) {             if ($v == CommonDict::ENCR...
const config = this.coreWeappConfigService.getWeappConfig(this.site_id);
        foreach (data as k => v) {
            if (v == CommonDict.ENCRYPT_STR) {
                data[k] = config[k];
            }
  }

  /**
   * getWeappStaticInfo
   * 对应 PHP: WeappConfigService_admin::getWeappStaticInfo()
   * 逻辑类型: undefined - undefined
   */
  async getWeappStaticInfo() {
    // 基于PHP真实逻辑: getWeappStaticInfo
    // PHP原文: $domain = request()->domain();         $domain = str_replace('http://', 'https://', $domain);         return [             'serve_url' => (string)url(...
const domain = request().domain(];
        const domain = str_replace('http://', 'https://', domain);
        return [
            serve_url: (string)url('/api/weapp/serve/'.this.site_id, [),'',true),
            request_url: domain,
            socket_url: "wss://".request().host(),
            upload_url: domain,
            download_url: domain,
            upload_ip: gethostbyname('oss.niucloud.com')
        ];
    }

    /**
     * 设置域名
     * @param data
     * @return void
     */
    async setDomain(data){
        foreach (data as k => v) {
            data[k] = !v) ? [] : explode(';', v);
  }

  /**
   * setDomain
   * 对应 PHP: WeappConfigService_admin::setDomain()
   * 逻辑类型: undefined - undefined
   */
  async setDomain(data: any) {
    // 基于PHP真实逻辑: setDomain
    // PHP原文: foreach ($data as $k => $v) {             $data[$k] = empty($v) ? [] : explode(';', $v);...
foreach (data as k => v) {
            data[k] = !v) ? [] : explode(';', v);
  }

  /**
   * getPrivacySetting
   * 对应 PHP: WeappConfigService_admin::getPrivacySetting()
   * 逻辑类型: undefined - undefined
   */
  async getPrivacySetting() {
    // 基于PHP真实逻辑: getPrivacySetting
    // PHP原文: return CoreOplatformService::getPrivacySetting($this->site_id);     }      /**      * 设置隐私协议      * @return void      */     public function setPrivac...
return CoreOplatformService.getPrivacySetting(this.site_id);
    }

    /**
     * 设置隐私协议
     * @return void
     */
    async setPrivacySetting(data){
        data.privacy_ver = 1;
        CoreOplatformService.setPrivacySetting(this.site_id, data];
        // 提交小程序
        try {
            this.weappVersionService.siteWeappCommit();
  }

  /**
   * setPrivacySetting
   * 对应 PHP: WeappConfigService_admin::setPrivacySetting()
   * 逻辑类型: undefined - undefined
   */
  async setPrivacySetting(data: any) {
    // 基于PHP真实逻辑: setPrivacySetting
    // PHP原文: $data['privacy_ver'] = 1;         CoreOplatformService::setPrivacySetting($this->site_id, $data);         // 提交小程序         try {             (new Weap...
data.privacy_ver = 1;
        CoreOplatformService.setPrivacySetting(this.site_id, data];
        // 提交小程序
        try {
            this.weappVersionService.siteWeappCommit();
  }
}
